import request from "@/utils/request";
import axios from "axios";
import store from "@/store/index";
import { getToken } from "@/utils/auth";
import host from "@/utils/host";

export default {
  borrowOrReturn(data) {
    return request({
      url: `/borrow/borrowAndReturn`,
      method: "post",
      data,
    });
  },
  list(currentPage, pageSize, search) {
    if (search) {
      return request({
        url: `/borrow/list/${currentPage}/${pageSize}/${search}`,
        method: "get",
      });
    }
  },
  total(search) {
    if (search) {
      return request({
        url: `/borrow/total/${search}`,
        method: "get",
      });
    }
  },
  unreturned(currentPage, pageSize, empno, airplane) {
    let empnoParam = empno || 0;
    let airplaneParam = airplane || "_";
    return request({
      url: `/borrow/unreturned/${currentPage}/${pageSize}/${empnoParam}/${airplaneParam}`,
      method: "get",
    });
  },
  unreturnedTotal(empno, airplane) {
    let empnoParam = empno || 0;
    let airplaneParam = airplane || "_";
    return request({
      url: `/borrow/unreturnedTotal/${empnoParam}/${airplaneParam}`,
      method: "get",
    });
  },
  //用于给打印机显示的方法
  printForList(empno) {
    empno = empno || 0;
    return request({
      url: `/borrow/printForList/${empno}`,
      method: "get",
    });
  },
  download(empno, airplane) {
    let jwt = "your_jwt_string";
    if (store.getters.token) {
      jwt = getToken();
    }
    empno = empno || 0;
    airplane = airplane || "_";
    const url = `http://${host}:8081/T01/borrow/download/${empno}/${airplane}`;
    axios
      .get(url, {
        headers: {
          Authorization: `${jwt}`, // 在请求头中添加 JWT 字符串
          "Content-Type": "application/json", // 在请求头中添加 Content-Type
        },
        responseType: "blob", // 指定响应类型为二进制数据
      })
      .then((response) => {
        const blob = new Blob([response.data], {
          type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // 设置为正确的 MIME 类型
        });
        const url = window.URL.createObjectURL(blob);
        const link = document.createElement("a");
        link.href = url;
        let data;
        //这里empno和airplane不可能同时为0和_
        if (empno != 0 && airplane != "_") {
          data = empno + "+" + airplane;
        } else {
          if (empno == 0) {
            data = airplane;
          } else {
            data = empno;
          }
        }
        link.setAttribute("download", `${data}的未归还记录.xlsx`);
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      })
      .catch((error) => {
        alert("下载文件出错：" + error.message || error.data);
      });
  },
  printPage: async function (empno) {
    const res = await axios.get(
      `http://${host}:8081/T01/print/printBorrow/${empno}`
    );
    if (!res.data.code) {
      const win = window.open("TMS");
      win.document.write(res.data);
      win.print();
      win.close();
    } else {
      alert(res.data.message)
    }
  },
};
